import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *根据指定信息进行查询
 */
public class Search {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static <T> void search(List<Map<String, Object>> em, String instruction) {
        Scanner scanner = new Scanner(System.in);
        if ("select".equals(instruction)) {
            try {
                System.out.println("请问你想通过那些信息查询对应的员工信息，例如：id:1 或者 time:2022-01-01 2023-01-01");
                String s = scanner.nextLine();
                String[] split1 = s.split(":");
                String front = split1[0];
                String latter = split1[1];
                switch (front) {
                    case "id":
                        for (Map<String, Object> stringObjectMap : em) {
                            if (latter.equals(stringObjectMap.get("id").toString())) {
                                System.out.println("对应的员工信息：" + stringObjectMap);
                            }
                        }
                        break;
                    case "name":
                        for (Map<String, Object> stringObjectMap : em) {
                            if (latter.equals(stringObjectMap.get("name"))) {
                                System.out.println("对应的员工信息：" + stringObjectMap);
                            }
                        }
                        break;
                    case "job":
                        for (Map<String, Object> stringObjectMap : em) {
                            if (latter.equals(stringObjectMap.get("job"))) {
                                System.out.println("对应的员工信息：" + stringObjectMap);
                            }
                        }
                        break;
                    case "time":
                        try {
                            // latter -> "2022-01-01 2023-01-01"
                            String[] s1 = latter.split(" ");
                            // 开始时间 s1[0] -> "2022-01-01"
                            Date startDate = simpleDateFormat.parse(s1[0]);
                            // 结束时间 s2[0] -> "2023-01-01"
                            Date endDate = simpleDateFormat.parse(s1[1]);

                            // 确保开始日期不晚于结束日期
                            if (startDate.after(endDate)) {
                                System.out.println("开始日期不能晚于结束日期！");
                                break;
                            }
                            // 遍历员工信息，查找入职时间在范围内的记录
                            for (Map<String, Object> stringObjectMap : em) {
                                Object timeObj = stringObjectMap.get("time");
                                //将string类型强转为date
                                Date time = simpleDateFormat.parse((String) timeObj);
                                    if (time.after(startDate) && time.before(endDate)) {
                                        System.out.println("对应的员工信息：" + stringObjectMap);
                                    } else {
                                        System.out.println("没有找到符合条件的员工信息");
                                    }
                                }
                            break;
                        } catch (ParseException e) {
                            e.printStackTrace();
                            System.out.println("日期格式错误，请输入正确的日期格式（yyyy-MM-dd）");
                        }
                    default:
                        System.out.println("查询格式错误");
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("查询格式错误,严格按照id:1或者time:2022-01-01 2023-01-01格式");
            }
        }
    }
}
